package {{basePackage}}.controller;

import {{basePackage}}.entity.{{entityName}};
import {{basePackage}}.service.{{entityName}}Service;
import {{basePackage}}.dto.{{entityName}}DTO;
import {{basePackage}}.vo.{{entityName}}VO;
import {{basePackage}}.common.Result;
import {{basePackage}}.common.PageResult;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * {{tableComment}} 控制器
 * 
 * @author {{author}}
 * @version {{version}}
 * @since {{createTime}}
 */
@RestController
@RequestMapping("/api/{{#lowerCase}}{{entityName}}{{/lowerCase}}")
@Tag(name = "{{tableComment}}管理", description = "{{tableComment}}相关的增删改查操作")
@Validated
public class {{entityName}}Controller {

    @Autowired
    private {{entityName}}Service {{#lowerCase}}{{entityName}}{{/lowerCase}}Service;

    /**
     * 分页查询{{tableComment}}
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询{{tableComment}}", description = "根据条件分页查询{{tableComment}}列表")
    @ApiResponse(responseCode = "200", description = "查询成功")
    public Result<PageResult<{{entityName}}VO>> page(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "页面大小", example = "10") @RequestParam(defaultValue = "10") Integer pageSize{{#if queryFields}},
            {{#each queryFields}}
            @Parameter(description = "{{comment}}") @RequestParam(required = false) {{javaType}} {{fieldName}}{{#unless @last}},{{/unless}}
            {{/each}}{{/if}}
    ) {
        {{entityName}}DTO queryDTO = new {{entityName}}DTO();
        {{#each queryFields}}
        queryDTO.set{{#upperFirst}}{{fieldName}}{{/upperFirst}}({{fieldName}});
        {{/each}}
        
        PageResult<{{entityName}}VO> result = {{#lowerCase}}{{entityName}}{{/lowerCase}}Service.page(pageNum, pageSize, queryDTO);
        return Result.success(result);
    }

    /**
     * 根据ID查询{{tableComment}}
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询{{tableComment}}详情", description = "根据ID查询{{tableComment}}详细信息")
    @ApiResponse(responseCode = "200", description = "查询成功")
    @ApiResponse(responseCode = "404", description = "{{tableComment}}不存在")
    public Result<{{entityName}}VO> getById(
            @Parameter(description = "{{tableComment}}ID", required = true) @PathVariable @NotNull Long id
    ) {
        {{entityName}}VO result = {{#lowerCase}}{{entityName}}{{/lowerCase}}Service.getById(id);
        return Result.success(result);
    }

    /**
     * 新增{{tableComment}}
     */
    @PostMapping
    @Operation(summary = "新增{{tableComment}}", description = "创建新的{{tableComment}}")
    @ApiResponse(responseCode = "200", description = "创建成功")
    @ApiResponse(responseCode = "400", description = "参数验证失败")
    public Result<{{entityName}}VO> create(
            @Parameter(description = "{{tableComment}}信息") @RequestBody @Valid {{entityName}}DTO {{#lowerCase}}{{entityName}}{{/lowerCase}}DTO
    ) {
        {{entityName}}VO result = {{#lowerCase}}{{entityName}}{{/lowerCase}}Service.create({{#lowerCase}}{{entityName}}{{/lowerCase}}DTO);
        return Result.success(result);
    }

    /**
     * 更新{{tableComment}}
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新{{tableComment}}", description = "根据ID更新{{tableComment}}信息")
    @ApiResponse(responseCode = "200", description = "更新成功")
    @ApiResponse(responseCode = "404", description = "{{tableComment}}不存在")
    public Result<{{entityName}}VO> update(
            @Parameter(description = "{{tableComment}}ID", required = true) @PathVariable @NotNull Long id,
            @Parameter(description = "{{tableComment}}信息") @RequestBody @Valid {{entityName}}DTO {{#lowerCase}}{{entityName}}{{/lowerCase}}DTO
    ) {
        {{entityName}}VO result = {{#lowerCase}}{{entityName}}{{/lowerCase}}Service.update(id, {{#lowerCase}}{{entityName}}{{/lowerCase}}DTO);
        return Result.success(result);
    }

    /**
     * 删除{{tableComment}}
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除{{tableComment}}", description = "根据ID删除{{tableComment}}")
    @ApiResponse(responseCode = "200", description = "删除成功")
    @ApiResponse(responseCode = "404", description = "{{tableComment}}不存在")
    public Result<Void> delete(
            @Parameter(description = "{{tableComment}}ID", required = true) @PathVariable @NotNull Long id
    ) {
        {{#lowerCase}}{{entityName}}{{/lowerCase}}Service.delete(id);
        return Result.success();
    }

    /**
     * 批量删除{{tableComment}}
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除{{tableComment}}", description = "根据ID列表批量删除{{tableComment}}")
    @ApiResponse(responseCode = "200", description = "删除成功")
    public Result<Void> batchDelete(
            @Parameter(description = "{{tableComment}}ID列表") @RequestBody @Valid List<Long> ids
    ) {
        {{#lowerCase}}{{entityName}}{{/lowerCase}}Service.batchDelete(ids);
        return Result.success();
    }

    {{#if hasRelations}}
    {{#each relations}}
    /**
     * 查询{{../tableComment}}的{{comment}}
     */
    @GetMapping("/{id}/{{#lowerCase}}{{relatedEntity}}{{/lowerCase}}")
    @Operation(summary = "查询{{../tableComment}}的{{comment}}", description = "根据{{../tableComment}}ID查询关联的{{comment}}")
    public Result<List<{{relatedEntity}}VO>> get{{relatedEntity}}s(
            @Parameter(description = "{{../tableComment}}ID", required = true) @PathVariable @NotNull Long id
    ) {
        List<{{relatedEntity}}VO> result = {{#lowerCase}}{{../entityName}}{{/lowerCase}}Service.get{{relatedEntity}}s(id);
        return Result.success(result);
    }
    {{/each}}
    {{/if}}
}
